package com.piggy.msg.server.controller;

import cn.hutool.json.JSONUtil;
import com.piggy.common.core.constant.SecurityConstants;
import com.piggy.common.core.utils.sms.SmsConfig;
import com.piggy.common.core.web.domain.AjaxResult;
import com.piggy.msg.domain.bo.SmsMessage;
import com.piggy.msg.server.dispatcher.MessageDispatcher;
import com.piggy.msg.service.ISmsClient;
import com.piggy.system.api.RemoteISysSmsService;
import com.piggy.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 推送通知
 *
 * @author woodev
 */
@RestController
@Api(value = "短信", tags = "短信")
public class SmsController implements ISmsClient {


    @Autowired
    private MessageDispatcher dispatcher;

    @Autowired
    private RemoteISysSmsService sysSmsService;

    /**
     * 短信通知
     *smsMessage
     * @return
     */
    @ApiOperation(value = "发送短信", notes = "发送短信")
    @PostMapping("/sms")
    @Override
    public AjaxResult send(@RequestBody  SmsMessage smsMessage) {
        this.dispatcher.dispatch(smsMessage);
        return AjaxResult.success();
    }

    @Override
    @ApiOperation(value = "批量发送短信", notes = "批量发送短信")
    @PostMapping("/sendBatchSms")
    public AjaxResult sendBatchSms(SmsMessage messages) {
        this.dispatcher.dispatch(messages);
        return AjaxResult.success();
    }

    /**
     * 获取消息详细信息
     */
    @ApiOperation("获取短信配置")
    @GetMapping("/config")
    public AjaxResult getConfig() {
        R<SmsConfig> config = sysSmsService.getConfig(SecurityConstants.INNER);
        return AjaxResult.success(JSONUtil.toJsonStr(config.getData()));
    }

}
